<template>
  <Treeselect v-model="model"
              :class="className"
              :flat="multiple"
              :multiple="multiple"
              :autoDeselectDescendants="multiple"
              :autoSelectDescendants="multiple"
              :autoSelectAncestors="multiple"
              :clearable="clearable"
              :options="data">
    <span slot="option-label" style="font-size: 18px" slot-scope="{ node }">{{ node.raw.name }}</span>
    <span slot="value-label" slot-scope="{node}">{{ node.raw.name }}</span>
  </Treeselect>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'TreeSelect',
  components: { Treeselect },
  props: {
    clearable: {
      type: Boolean,
      default: true
    },
    value:null,
    className:'',
    multiple:{
      type:Boolean,
      default: false
    },
    dataSource:{
      type:Function,
      default:function(){
        return new Promise(function(resolve, reject){
         try{
           resolve();
         }catch (e){
           reject(e)
         }
        });
      }
    },
    condition:{
      type:Object,
      default:function(){
        return {}
      }
    }
  },
  computed:{
    model:{
      get(){
        return this.$props.value;
      },
      set(newVal){
        this.$emit('input', newVal)
      },
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      data: [],
      visible: false
    }
  },
  mounted() {
    this.changeList()
  },
  methods: {
    changeList() {
      this.$props.dataSource(this.$props.condition).then(res => {
        this.data = res.data
      })
    }
  }
}
</script>

<style scoped>

</style>
